<template>
  <transition name="animate">
    <div class="layout" v-if="visible">
      <div class="layout-mask" ref="mask"></div>
      <div class="layout-content">
        <div class="inner">
          <div class="close" @click="close">
            <i class="icon iconfont icon-guanbi"> </i>
          </div>
          <keep-alive>
            <component
              :is="componentType"
              @changeComType="(type) => (componentType = type)"
            ></component>
          </keep-alive>
        </div>
      </div>
    </div>
  </transition>
</template>

<script>
import selectTab from './components/select-tab.vue'
import Login from './components/login.vue'
import Register from './components/register.vue'
import ResetPassword from './components/reset-password.vue'
export default {
  data() {
    return {
      componentType: 'login', // login or register or resetPassword
      visible: false,
    }
  },
  methods: {
    open(type) {
      this.componentType = type
      this.visible = true
    },
    close() {
      const LoginModule = require('./login.js').default
      const loginModule = new LoginModule()
      this.visible = false
      this.$refs.mask.addEventListener('transitionend', () => {
        loginModule.close()
      })
    }
  },
  components: {
    selectTab,
    Login,
    Register,
    ResetPassword
  }
}
</script>

<style scoped lang="less">
.layout {
  z-index: 100;
  position: fixed;
  top: 0;
  bottom: 0;
  width: 100%;
  height: 100%;

  .layout-mask {
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
  }

  .layout-content {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 100%;
    max-width: 500px;
    max-height: 100%;
    overflow-y: auto;
    transform: translate3d(-50%, -50%, 0) scale(1);
    transform-origin: 50% 50%;

    .inner {
      position: relative;
      // height: 550px;
      width: 100%;
      padding: 36px;
      box-sizing: border-box;
      background-color: #333542;
      border-radius: 5px;
      overflow: hidden;

      .close {
        position: absolute;
        right: 0;
        top: 0;
        padding: 10px;
        font-size: 0;
        .icon {
          font-size: 12px;
          font-weight: 600;
          color: #cacaca;
        }
      }
    }
  }
}

// animation css
.animate-leave-active,
.animate-enter-active {
  transition: 0.3s all;

  .layout-mask {
    transition: 0.3s all;
  }
  .layout-content {
    transition: 0.3s all;
  }
}

.animate-enter,
.animate-leave-to {
  .layout-mask {
    opacity: 0;
  }
  .layout-content {
    transform: translate3d(-50%, -50%, 0) scale(0);
  }
}
</style>